package com.rfid.afd.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.rfid.afd.activity.adapter.DeleteAdapter;
import com.rfid.afd.activity.listener.CheckRfidListener;
import com.rfid.afd.activity.view.IDeleteView;
import com.rfid.afd.presenter.DeletePresenter;
import com.rfid.db.bean.Rfid;
import com.rfid.rls_psw.R;

import org.w3c.dom.Text;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
/**剔除，表示要删除书籍的相关信息，本地剔除后，在同步的时候告知服务器*/
public class DeleteActivity extends BaseActivity<DeletePresenter> implements TextWatcher,IDeleteView, CheckRfidListener {

    @BindView(R.id.edit)
    EditText mEdit;
    @BindView(R.id.recyclerview)
    RecyclerView mRecyclerView;
    @BindView(R.id.quit)
    Button mQuit;

    DeleteAdapter mAdapter;
    private List<Rfid> mRfids;
    private List<Rfid> mSelects;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_delete);
        ButterKnife.bind(this);

        mRfids = new ArrayList<>();
        mSelects = new ArrayList<>();
        mEdit.addTextChangedListener(this);
        mAdapter = new DeleteAdapter(this,mRfids);
        mAdapter.setCheckRfidListener(this);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerView.addItemDecoration(new DividerItemDecoration(this,RecyclerView.VERTICAL));
        LayoutAnimationController controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
        mRecyclerView.setLayoutAnimation(controller);
        mPresenter.queryDeleteRfids();
    }

    @Override
    public DeletePresenter getPresenter() {
        return new DeletePresenter();
    }

    boolean scan;
    public void scan(View view) {
        TextView textView = (TextView) view;
        if(scan) {
            mPresenter.stopScanRfid();
            textView.setText("开始扫描");
            mQuit.setEnabled(true);
        } else {
            mPresenter.startScanRfid();
            textView.setText("停止扫描");
            mQuit.setEnabled(false);
        }
        scan = !scan;
    }

    public void delete(View view) {
        mPresenter.delete(mSelects);
    }

    public void quit(View view) {
        onBackPressed();
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
    }

    @Override
    public void afterTextChanged(Editable s) {
//        String text = s.toString();
//        mRfids.clear();
//        mAdapter.notifyDataSetChanged();
//        if(TextUtils.isEmpty(text)) {
//            return;
//        }
//        mPresenter.queryRfidsByBookName(text);
    }

    @Override
    public void onRfidsByBookName(List<Rfid> rfids) {
//        mRfids.clear();
//        mRfids.addAll(rfids);
//        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onScanedRfid(String code, Rfid rfid) {
        if(!mRfids.contains(rfid)) {
            mRfids.add(rfid);
            mAdapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onDeleteComplete(List<Rfid> rfids) {
        mRfids.removeAll(rfids);
        mSelects.clear();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onDeleteQuery(List<Rfid> deletes) {
        mRfids.addAll(deletes);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLoading() {
    }

    @Override
    public void stopLoading() {
    }

    @Override
    public void onChecked(boolean check, Rfid rfid) {
        if(check) {
            mSelects.add(rfid);
        } else {
            mSelects.remove(rfid);
        }
    }

    @Override
    public void finish() {
        super.finish();
        mPresenter.stopScanRfid();
    }

    @Override
    public void onBackPressed() {
        if(scan) {
            return;
        }
        super.onBackPressed();
        mPresenter.stopScanRfid();
    }
}